<?php
include 'config.inc.php';
$con = mysql_connect($dbhost, $dbuser, $dbpw);
mysql_select_db($dbname, $con);
mysql_query("set names ".$dbcharset);
 
function InsertActivity($hash, $name,  $password, $time, $title, $description , $userId = 0, $type = 0)
{
    $sql = "INSERT INTO `hagt`.`activity` (`Hash`, `Name`,  `UserId`, `Password`, `Time`, `Type`, `Title`, `Description`) VALUES (unhex('$hash'), '$name', $userId, '$password','$time', $type, '$title', '$description');";
    mysql_query($sql);
}


function InsertComment($activityId, $userId, $name, $content, $time, $statue)
{
    $sql = "INSERT INTO `hagt`.`comment` (`ActivityId`, `UserId`, `Name`, `Content`, `Time`, `Statue`) VALUES ($activityId, $userId, '$name', '$content', '$time', $statue);";
    mysql_query($sql);
}

function InsertVote($activityId, $option)
{
    $sql = "INSERT INTO `hagt`.`vote` (`ActivityId`, `Option`) VALUES ($activityId, '$option');";
    mysql_query($sql);
}

function InsertVoteData($activityId, $name, $voteData, $time, $userId = 0)
{
    $sql = "INSERT INTO `hagt`.`voteData` (`ActivityID`, `UserId`, `Name`, `Time`, `VoteData`) VALUES ('$activityId', $userId, '$name', '$time', $voteData);";
    mysql_query($sql);
    
}

function DeleteActivity($id)
{
	$sql = "DELETE FROM `hagt`.`activity` WHERE `Id` = $id";
	mysql_query($sql);
   
}

function DeleteComment($id)
{
	$sql = "DELETE FROM `hagt`.`comment` WHERE `Id` = $id";
	mysql_query($sql);
   
}

function DeleteVote($id)
{
	$sql = "DELETE FROM `hagt`.`vote` WHERE `Id` = $id";
	mysql_query($sql);
    
}

function DeleteVoteData($id)
{
	$sql = "DELETE FROM `hagt`.`voteData` WHERE `Id` = $id";
	mysql_query($sql);
   
}

//function UpdateActivity()
{
	
	
}

//
function GetActivityByHash($hash)
{
	$sql = "SELECT * FROM `hagt`.`activity` WHERE `Hash` = unhex('$hash')";
	$result = mysql_query($sql);
	$arr = mysql_fetch_array($result);
	return $arr;
    
}

function GetCommentsByActivity($id)
{
	$sql = "SELECT * FROM `hagt`.`comment` WHERE `ActivityId` = $id Order By `Time`";
	$result = mysql_query($sql);
	$arr = array();
	while ($info = mysql_fetch_array($result))
	{
		array_push($arr,$info);	
	}
	return $arr;
    
}

function GetVoteDatasByActivity($id)
{
	$sql = "SELECT * FROM `hagt`.`votedata` WHERE `ActivityId` = $id Order By `Time`";
	$result = mysql_query($sql);
    $arr = array();
	while ($info = mysql_fetch_array($result))
	{
		array_push($arr,$info);	
	}
	return $arr;
    
}

function GetVotesByActivity($id)
{
	$sql = "SELECT * FROM `hagt`.`vote` WHERE `ActivityId` = $id";
	$result = mysql_query($sql);
    $arr = array();
	while ($info = mysql_fetch_array($result))
	{
		array_push($arr,$info);	
	}
	return $arr;
    
}